import datetime as dt
from pathlib import Path
from tqdm import tqdm
import logging
import polars as pl
import pickle

from cryptoAlpha.dataFeed import BinanceDataFeed

if __name__ == "__main__":
    feed = BinanceDataFeed(is_testNet=False, logLevel=logging.ERROR)
        
    dataDir = Path("data")
    
    startTime = dt.datetime(2024,8,1)
    endTime = dt.datetime(2025,1,1)
    
    try:
        with open("saved_kbar.pk", "rb") as f:
            saved = pickle.load(f)
    except Exception as e:
        saved = {}
        print(e)
    
    info = feed.get_exchange_info()

    interval = "1"
    period = "m"
    dataDir = dataDir.joinpath(period + interval)
    dataDir.mkdir(parents=True, exist_ok=True)
    for i in tqdm(info["symbols"]):
        if i["quoteAsset"] != "USDT":
            continue
        symbol = i["symbol"]
        
        # 逐月下载
        t = startTime
        while t <= endTime:
            if t.date() not in saved.get(symbol, []):
                r = feed.get_klines(symbol, interval + period, t, t.replace(hour=23, minute=59, second=59, microsecond=999999))
                if r is not None and len(r) > 0:
                    r.write_parquet(dataDir.as_posix(), partition_by=["exchange","trading_date","symbol"])
                    print(f"{symbol} save {t}")
            t = t + dt.timedelta(days=1)
    print("done")
        
